#
# OpenSistemas::Zabbix::Hosts
# Copyright (C) 2010 Open Sistemas <imasd@opensistemas.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
package OpenSistemas::Zabbix::Hosts;

use strict;
use warnings;

use Carp;
use OpenSistemas::OID::Item;
use NetSNMP::ASN (':all');
use DateTime;

use base 'OpenSistemas::OID::Tree';

my $hostName     = '1.1.2.1';
my $hostID       = '1.1.2.2.1.1';
my $monitorDesc  = '1.1.2.2.1.2';
my $monitorType  = '1.1.2.2.1.3';
my $monitorNum   = '1.1.2.2.1.4';
my $monitorStr   = '1.1.2.2.1.5';
my $monitorGau   = '1.1.2.2.1.6';
my $monitorLast  = '1.1.2.2.1.7';

sub new
{
	my ($pkg, %params) = @_;

  croak 'Hosts: dhb parameter missing' if !exists ($params{dbh});
  croak 'Hosts: baseOID parameter missing' if !exists ($params{baseOID});

	my $self = {
		%params
	};

	bless $self, $pkg;

	return $self;
}

sub update
{
  my $self = shift;
  my $dbh = $self->{dbh};
  my $i;

  my $sth = $dbh->prepare("select distinct h.host as hostName from hosts h where h.status=0;");
  return 0 if !$sth;
  return 0 if !$sth->execute();
  $i = 0;
  while(my $ref = $sth->fetchrow_hashref())
  {
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$hostName .'.' .$i,
      asn => ASN_OCTET_STR,
      value => $ref->{hostName}
      }));
    $i++;
  }
  $sth->finish();

  $sth = $dbh->prepare("select h.host as hostID, i.key_ as monitorDesc, i.value_type as monitorType, i.lastvalue as monitorValue, i.lastclock as monitorLast from hosts h, items i where h.status=0 and h.hostid=i.hostid and i.status=0;");
  return 0 if !$sth;
  return 0 if !$sth->execute();
  $i = 0;
  while(my $ref = $sth->fetchrow_hashref())
  {
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$hostID .'.' .$i,
      asn => ASN_OCTET_STR,
      value => $ref->{hostID}
      }));
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$monitorDesc .'.' .$i,
      asn => ASN_OCTET_STR,
      value => $ref->{monitorDesc}
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$monitorNum .'.' .$i,
      asn => ASN_OCTET_STR,
      value => 0
      }));
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$monitorStr .'.' .$i,
      asn => ASN_OCTET_STR,
      value => ''
      }));
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$monitorGau .'.' .$i,
      asn => ASN_GAUGE,
      value => 0
      }));

    if($ref->{monitorType} eq 0)
    {
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorNum .'.' .$i,
        asn => ASN_OCTET_STR,
        value => $ref->{monitorValue}
        }));
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorType .'.' .$i,
        asn => ASN_INTEGER,
        value => 0
        }));
    }
    elsif($ref->{monitorType} eq 3)
    {
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorGau .'.' .$i,
        asn => ASN_GAUGE,
        value => $ref->{monitorValue}
        }));
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorType .'.' .$i,
        asn => ASN_INTEGER,
        value => 2
        }));
    }
    else
    {
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorStr .'.' .$i,
        asn => ASN_OCTET_STR,
        value => $ref->{monitorValue}
        }));
      $self->set(OpenSistemas::OID::Item->new({
        oid => $self->{baseOID} .'.' .$monitorType .'.' .$i,
        asn => ASN_INTEGER,
        value => 1
        }));
    }

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$monitorLast .'.' .$i,
      asn => ASN_OCTET_STR,
      value => $ref->{monitorLast} ? DateTime->from_epoch(epoch => $ref->{monitorLast})->datetime : ''
      }));

    $i++;
  }
  $sth->finish();

  return 1;
}

1;
